﻿using Minecraft.Config;
using Minecraft.Model;
using Minecraft.Model.ReqResp;
using SuperSocket.SocketBase.Command;
using SuperSocket.SocketBase.Protocol;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Minecraft.ServerHall.Cmd
{
	public class EquipmentGoodsSelect : CommandBase<MinecraftSession, StringRequestInfo>
	{
		public override string Name
		{
			get
			{
				return ProtocolHelper.GetProtocolStr(defCommand);
			}
		}
		private EnumCommand defCommand = EnumCommand.Equipment_EquipmentGoodsSelect;
		public override void ExecuteCommand(MinecraftSession session, StringRequestInfo requestInfo)
		{
			var req = requestInfo.GetRequestObj<EquipmentGoodsSelectReq>(session);
			if (req == null)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = "错误1：（原因：请求实体为空）" });
				return;
			}
			if (!session.minecraftSessionInfo.IsLogin)
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = "错误1：（原因：未登录）" });
				return;
			}

			//if (JsonConfig.Value.IsDebug)
			//{
			//	req = new EquipmentGoodsSelectReq
			//	{
			//		enumGoodsBelongsToType = EnumGoodsBelongsToType.SurvivorEquipment,
			//		guid = "",
			//	};
			//}

			//装备类型判断
			if (!MinecraftConfig.equipmentBelongsToTypeList.Any(m => m == req.enumGoodsBelongsToType))
			{
				session.Send(defCommand, req, new BaseResp { RespLevel = RespLevelEnum.Error, Msg = $"错误1：（原因：装备来源不正确，前端传入的装备来源为【{req.enumGoodsBelongsToType.ToString()}】）" });
				return;
			}
			if (req.guid.IsNullOrWhiteSpace())
			{
				req.guid = "";
			}

			//暂时写死
			if (req.enumGoodsBelongsToType == EnumGoodsBelongsToType.SurvivorEquipment)
			{
				req.guid = session.minecraftSessionInfo.survivorList.FirstOrDefault(m => m.PlayerId == session.minecraftSessionInfo.player.PlayerId).SurvivorId.ToString();
			}

			var equipmentList = (from info in session.minecraftSessionInfo.goodsList
								 where info.enumGoodsBelongsToType_Convert == req.enumGoodsBelongsToType && info.guid == req.guid
								 select info).ToList().GetBackpackGoodsCellInfo();

			var resp = new EquipmentGoodsSelectResp
			{
				backpackCellInfoList = equipmentList,
				enumGoodsBelongsToType = req.enumGoodsBelongsToType,
				guid = req.guid
			};
			session.Send(defCommand, req, resp);
		}
	}
}
